<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/9/19
 * Time: 9:00
 */
namespace frontend\models;

use yii;
use yii\db\ActiveRecord;

class Cart extends ActiveRecord{

    public function scenarios(){
        return [
            'create' => ['uid', 'gid', 'create_time', 'num'],

            'update' => ['num'],
        ];
    }

    public function rules(){
        return [
            ['uid', 'default', 'value' => yii::$app->user->getId(),],
            ['gid', 'exist', 'targetClass' => 'frontend\models\Goods', 'targetAttribute' => ['gid'=> 'id'],],
            ['create_time', 'default', 'value' => time(),],
        ];
    }

    public function attributeLabels(){
        return [
            'uid' => '用户ID',
            'gid' => '商品ID',
            'create_time' => '添加时间',
        ];
    }

    public function addNum($prama){
        if($prama > 0){
            $this->num = $this->num + $prama;
            return $this;
        }
        return false;
    }

    public function downNum($prama){
        if($this->num >= $prama){
            $this->num = $this->num - $prama;
            return $this;
        }
        return false;
    }
}